package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.bo.TopicParam;
import com.cskaoyan.bean.pojo.MallTopic;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.service.TopicService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author JJ
 * @description TopicController专题管理
 * @time 2021/7/9 17:41
 */
@RestController
@RequestMapping("admin/topic")
public class TopicController {

    @Autowired
    TopicService topicService;

    @RequestMapping("list")
    @RequiresPermissions("admin:topic:list")
    public BaseRespVo list(TopicParam param) {

        BaseData<MallTopic> mallTopicBaseData = topicService.listTopic(param);

        return BaseRespVo.ok(mallTopicBaseData);
    }

    @RequestMapping("create")
    @RequiresPermissions("admin:topic:create")
    public BaseRespVo create(@RequestBody MallTopic param) {

        Integer i = topicService.createTopic(param);
        if (i<=0){
            return BaseRespVo.fail();
        }

        List<MallTopic> mallTopics = topicService.queryTopic(param);
        MallTopic mallTopic = mallTopics.get(0);
        return BaseRespVo.ok(mallTopic);
    }

    @RequestMapping("update")
    @RequiresPermissions("admin:topic:update")
    public BaseRespVo update(@RequestBody MallTopic param) {

        Integer i = topicService.updateTopic(param);

        List<MallTopic> mallTopics = topicService.queryTopic(param);
        MallTopic mallTopic = mallTopics.get(0);

        return BaseRespVo.ok(mallTopic);
    }


    @RequestMapping("delete")
    @RequiresPermissions("admin:topic:delete")
    public BaseRespVo delete(@RequestBody MallTopic param) {

        Integer i = topicService.deleteTopic(param);
        if (i<=0){
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }

}
